package com.helper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import android.content.Context;

public class FilesHelper {
	Context c ;
	public FilesHelper(Context c){
		this.c = c;
	}
	public String getLastIp() {
		// Create a new output file stream
		String destPath = c.getFilesDir().getPath();
        destPath = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases/lastIp.txt";
		String ip = "";
		 File file = new File(destPath);
		try {
			InputStream fileIn = new FileInputStream(file);
			InputStreamReader InputRead = new InputStreamReader(fileIn);
			BufferedReader buffreader = new BufferedReader(InputRead);
			ip = buffreader.readLine();
			if(ip==null){
				ip = "";
			}
			fileIn.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return ip;
	}

	public  void setLastIp(String ip) {
		FileWriter fw;
		try {
			
			fw = new FileWriter(c.getDatabasePath("lastIp.txt"));
			PrintWriter pw = new PrintWriter(fw);
			pw.write("");
			pw.flush(); 
			pw.write(ip);
			pw.flush(); 
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
